using System.Collections;
using System.Collections.Generic;
using MyFrame.Load;
using UnityEngine;
using MyFrame.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;



namespace MyFrame.UI
{

    public class UIMgr
    {
        //单例
        private static UIMgr instance = new UIMgr();
        public static UIMgr Instance => instance;

        //存储面板  的容器
        private Dictionary<string, BasePanel> panelDict = new();

        //一开始就获得 canvas
        private Transform canvasTransform;

        private UIMgr()
        {
            canvasTransform = GameObject.Find("Canvas").transform;
            //让canvas过场景不移除
            GameObject.DontDestroyOnLoad(canvasTransform.gameObject);
        }

        //显示面板
        public T ShowPanel<T>() where T : BasePanel
        {
            //确保 泛型T 和 面板名称  一致   
            //如此约定 可通过泛型名得到面板名    再通过面板名得到面板
            string panelName = typeof(T).Name;

            //已存在
            if (panelDict.ContainsKey(panelName))
                return panelDict[panelName] as T;

            //动态创建预设体     类名就是面板名，所以直接加载
            GameObject panelObj = GameObject.Instantiate(Resources.Load<GameObject>("UI/" + panelName));
            panelObj.transform.SetParent(canvasTransform, false);

            //获取面板组件
            BasePanel panel = panelObj.GetComponent<T>();
            //存储面板
            panelDict.Add(panelName, panel);

            panel.ShowMe();

            return panel as T;
        }

        //隐藏面板
        public void HidePanel<T>(bool isFade = false) where T : BasePanel
        {
            string panelName = typeof(T).Name;

            if (panelDict.ContainsKey(panelName))
            {
                if (isFade)
                {
                    // panelDict[panelName].HideMe(() =>
                    // {
                    //     //淡出后销毁面板
                    //     GameObject.Destroy(panelDict[panelName].gameObject);
                    //     panelDict.Remove(panelName);
                    // });
                }
                else
                {
                    GameObject.Destroy(panelDict[panelName].gameObject);
                    panelDict.Remove(panelName);
                }
            }
        }

        //获取面板
        public T GetPanel<T>() where T : BasePanel
        {
            string panelName = typeof(T).Name;
            if (panelDict.ContainsKey(panelName))
                return panelDict[panelName] as T;
            else
                return null;
        }

        /// <summary>
        /// 给控件添加自定义事件监听
        /// </summary>
        /// <param name="control">控件对象</param>
        /// <param name="type">事件类型</param>
        /// <param name="callBack">事件的响应函数</param>
        public static void AddCustomEventListener(UIBehaviour control, EventTriggerType type, UnityAction<BaseEventData> callBack)
        {
            EventTrigger trigger = control.GetComponent<EventTrigger>();
            if (trigger == null)
                trigger = control.gameObject.AddComponent<EventTrigger>();

            EventTrigger.Entry entry = new EventTrigger.Entry();
            entry.eventID = type;
            entry.callback.AddListener(callBack);

            trigger.triggers.Add(entry);
        }
    }
}
